package com.nguyen.views;

import java.util.ArrayList;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nguyen.adapters.ListDishAdapter;
import com.nguyen.entities.Category;
import com.nguyen.entities.DishItem;
import com.nguyen.letcook.LetCookActivity;
import com.nguyen.letcook.R;
import com.nguyen.service.Service;

public class ListDishView extends RelativeLayout implements OnItemClickListener, OnClickListener{

	TextView txtTitle; // title is the category selected.
	ListView lstDish;
	Button btnCategory;
	ListDishAdapter mAdapter;	
	
	int mStart = 0, step = 5; // use to paging
	boolean canLoadMore = true;
	
	Context mContext;
	boolean isGetDataFromServer = true;	
	
	public ListDishView(Context context, boolean isGetDataFromServer, int categoryId) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.list_dish, this, true);
		txtTitle = (TextView) findViewById(R.id.txtDishTitle);
		btnCategory = (Button) findViewById(R.id.btnCategory);
		btnCategory.setOnClickListener(this);
		
		lstDish = (ListView) findViewById(R.id.listDish);
		mAdapter = new ListDishAdapter(context);
		lstDish.setAdapter(mAdapter);
		lstDish.setOnItemClickListener(this);
		
				
		Category category = new Category();
		category.init(categoryId, "", "", "");
		
		this.isGetDataFromServer = isGetDataFromServer;
		if(isGetDataFromServer)			
			loadDataFromServer(category);
		
		mContext = context;
		// select data from database and fill it to title and list view.
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
		
	}
	
	@Override
	public void onClick(View v){
		if(v == btnCategory){
			LetCookActivity activity = (LetCookActivity) mContext;
			activity.setListCategory();
		}
	}
	
	class GetDataFromServer extends AsyncTask<Void, Void, ArrayList<DishItem>>{
		
		@Override
		public void onPreExecute(){
			
		}
		
		@Override
		public ArrayList<DishItem> doInBackground(Void... params){
			Service service = new Service();
			ArrayList<DishItem> arrResult = service.getListDishByCategory(1, mStart, (mStart + step));
			if(arrResult.size() < step)
				canLoadMore = false;
			else
				mStart = mStart + step + 1;
			return arrResult;
		}
		
		@Override
		public void onPostExecute(ArrayList<DishItem> result){
			if(result != null){
				for(DishItem item : result){
					mAdapter.addItem(item);
				}
				mAdapter.notifyDataSetChanged();
			}
		}
	}

	public void loadDataFromServer(Category category){
		mCategory = category;
		if(canLoadMore)
			new GetDataFromServer().execute();
	}
	
	Category mCategory;
}
